package http_proxy_router

import (
	"gitee.com/zhoulvvv/my_go_gateway/controller"
	"gitee.com/zhoulvvv/my_go_gateway/http_proxy_middleware"
	"gitee.com/zhoulvvv/my_go_gateway/middleware"
	"github.com/gin-gonic/gin"
)

func InitRouter(middlewares ...gin.HandlerFunc) *gin.Engine {
	router := gin.Default() // 每次请求都会在控制台打印日志
	// TODO 优化点 1
	// router := gin.New() // 请求不会打印日志
	router.Use(middlewares...)
	router.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})

	oauthRouter := router.Group("/oauth")
	oauthRouter.Use(middleware.TranslationMiddleware())
	{
		controller.RegisterOAuthController(oauthRouter)
	}

	router.Use(
		http_proxy_middleware.HTTPAccessModeMiddleware(),         // 服务匹配
		http_proxy_middleware.HTTPFlowCountModeMiddleware(),      // QPS 统计
		http_proxy_middleware.HTTPFlowLimitModeMiddleware(),      // 服务端和客户端限流
		http_proxy_middleware.HTTPJwtOAuthTokenModeMiddleware(),  // token 校验中间件
		http_proxy_middleware.HTTPJwtFlowCountModeMiddleware(),   // 租户流量统计
		http_proxy_middleware.HTTPJwtFlowLimitModeMiddleware(),   // 租户流量限流
		http_proxy_middleware.HTTPWhiteListModeMiddleware(),      // IP 白名单
		http_proxy_middleware.HTTPBlackListModeMiddleware(),      // IP 黑名单
		http_proxy_middleware.HTTPHeaderTransferModeMiddleware(), // header 头转换
		http_proxy_middleware.HTTPStripUriModeMiddleware(),       // strip uri 去除
		http_proxy_middleware.HTTPUrlRewriteModeMiddleware(),     // url重写
		http_proxy_middleware.HTTPReverseProxyModeMiddleware())   // 服务代理
	return router
}
